# ============================================================================
# Table 8: Effect of source name among subgroups for poverty policy
# ============================================================================

# --- White respondents ---
data_poverty_white <- filter(.data = data, police == 0 & white == 1)
diff_poverty_white <- mean(data_poverty_white$support_policy[data_poverty_white$deandre == 1]) -
  mean(data_poverty_white$support_policy[data_poverty_white$deandre == 0])
mean_poverty_white <- mean(data_poverty_white$support_policy[data_poverty_white$deandre == 0])
sd_poverty_white <- sqrt(mean((data_poverty_white$support_policy[data_poverty_white$deandre == 0] - mean_poverty_white)^2))
N_poverty_white <- nrow(data_poverty_white)

n_deandre_poverty_white <- sum(data_poverty_white$deandre)
set.seed(seed = 09291992, kind = "L'Ecuyer-CMRG")
Omega_deandre_poverty_white <- obtain_permutation_matrix(declaration = declare_ra(N = N_poverty_white,
                                                                                  m = n_deandre_poverty_white),
                                                         maximum_permutations = n_sims)
null_dist_deandre_poverty_white <- sapply(X = 1:ncol(Omega_deandre_poverty_white),
                                            FUN = function(x) {
                                              mean(data_poverty_white$support_policy[Omega_deandre_poverty_white[,x] == 1]) -
                                                mean(data_poverty_white$support_policy[Omega_deandre_poverty_white[,x] == 0]) })
two_p_deandre_poverty_white <- min(1, 2 * min(mean(null_dist_deandre_poverty_white <= diff_poverty_white),
                                              mean(null_dist_deandre_poverty_white >= diff_poverty_white)))

# --- Nonwhite respondents ---
data_poverty_nonwhite <- filter(.data = data, police == 0 & white == 0)
diff_poverty_nonwhite <- mean(data_poverty_nonwhite$support_policy[data_poverty_nonwhite$deandre == 1]) -
  mean(data_poverty_nonwhite$support_policy[data_poverty_nonwhite$deandre == 0])
mean_poverty_nonwhite <- mean(data_poverty_nonwhite$support_policy[data_poverty_nonwhite$deandre == 0])
sd_poverty_nonwhite <- sqrt(mean((data_poverty_nonwhite$support_policy[data_poverty_nonwhite$deandre == 0] - mean_poverty_nonwhite)^2))
N_poverty_nonwhite <- nrow(data_poverty_nonwhite)

n_deandre_poverty_nonwhite <- sum(data_poverty_nonwhite$deandre)
set.seed(seed = 09291992, kind = "L'Ecuyer-CMRG")
Omega_deandre_poverty_nonwhite <- obtain_permutation_matrix(declaration = declare_ra(N = N_poverty_nonwhite,
                                                                                     m = n_deandre_poverty_nonwhite),
                                                            maximum_permutations = n_sims)
null_dist_deandre_poverty_nonwhite <- sapply(X = 1:ncol(Omega_deandre_poverty_nonwhite),
                                               FUN = function(x) {
                                                 mean(data_poverty_nonwhite$support_policy[Omega_deandre_poverty_nonwhite[,x] == 1]) -
                                                   mean(data_poverty_nonwhite$support_policy[Omega_deandre_poverty_nonwhite[,x] == 0]) })
two_p_deandre_poverty_nonwhite <- min(1, 2 * min(mean(null_dist_deandre_poverty_nonwhite <= diff_poverty_nonwhite),
                                                 mean(null_dist_deandre_poverty_nonwhite >= diff_poverty_nonwhite)))

# --- GOP respondents ---
data_poverty_gop <- filter(.data = data, police == 0 & republican == 1)
diff_poverty_gop <- mean(data_poverty_gop$support_policy[data_poverty_gop$deandre == 1]) -
  mean(data_poverty_gop$support_policy[data_poverty_gop$deandre == 0])
mean_poverty_gop <- mean(data_poverty_gop$support_policy[data_poverty_gop$deandre == 0])
sd_poverty_gop <- sqrt(mean((data_poverty_gop$support_policy[data_poverty_gop$deandre == 0] - mean_poverty_gop)^2))
N_poverty_gop <- nrow(data_poverty_gop)

n_deandre_poverty_gop <- sum(data_poverty_gop$deandre)
set.seed(seed = 09291992, kind = "L'Ecuyer-CMRG")
Omega_deandre_poverty_gop <- obtain_permutation_matrix(declaration = declare_ra(N = N_poverty_gop,
                                                                                m = n_deandre_poverty_gop),
                                                       maximum_permutations = n_sims)
null_dist_deandre_poverty_gop <- sapply(X = 1:ncol(Omega_deandre_poverty_gop),
                                          FUN = function(x) {
                                            mean(data_poverty_gop$support_policy[Omega_deandre_poverty_gop[,x] == 1]) -
                                              mean(data_poverty_gop$support_policy[Omega_deandre_poverty_gop[,x] == 0]) })
two_p_deandre_poverty_gop <- 2 * min(mean(null_dist_deandre_poverty_gop <= diff_poverty_gop),
                                     mean(null_dist_deandre_poverty_gop >= diff_poverty_gop))

# --- Non-GOP respondents ---
data_poverty_nongop <- filter(.data = data, police == 0 & republican == 0)
diff_poverty_nongop <- mean(data_poverty_nongop$support_policy[data_poverty_nongop$deandre == 1]) -
  mean(data_poverty_nongop$support_policy[data_poverty_nongop$deandre == 0])
mean_poverty_nongop <- mean(data_poverty_nongop$support_policy[data_poverty_nongop$deandre == 0])
sd_poverty_nongop <- sqrt(mean((data_poverty_nongop$support_policy[data_poverty_nongop$deandre == 0] - mean_poverty_nongop)^2))
N_poverty_nongop <- nrow(data_poverty_nongop)

n_deandre_poverty_nongop <- sum(data_poverty_nongop$deandre)
set.seed(seed = 09291992, kind = "L'Ecuyer-CMRG")
Omega_deandre_poverty_nongop <- obtain_permutation_matrix(declaration = declare_ra(N = N_poverty_nongop,
                                                                                   m = n_deandre_poverty_nongop),
                                                          maximum_permutations = n_sims)
null_dist_deandre_poverty_nongop <- sapply(X = 1:ncol(Omega_deandre_poverty_nongop),
                                             FUN = function(x) {
                                               mean(data_poverty_nongop$support_policy[Omega_deandre_poverty_nongop[,x] == 1]) -
                                                 mean(data_poverty_nongop$support_policy[Omega_deandre_poverty_nongop[,x] == 0]) })
two_p_deandre_poverty_nongop <- min(1, 2 * min(mean(null_dist_deandre_poverty_nongop <= diff_poverty_nongop),
                                               mean(null_dist_deandre_poverty_nongop >= diff_poverty_nongop)))

diff_poverty_white_str <- fmt(x = diff_poverty_white, digits = 4, pval = two_p_deandre_poverty_white)
diff_poverty_nonwhite_str <- fmt(x = diff_poverty_nonwhite, digits = 4, pval = two_p_deandre_poverty_nonwhite)
diff_poverty_gop_str <- fmt(x = diff_poverty_gop, digits = 4, pval = two_p_deandre_poverty_gop)
diff_poverty_nongop_str <- fmt(x = diff_poverty_nongop, digits = 4, pval = two_p_deandre_poverty_nongop)

mean_poverty_white_str <- fmt(x = mean_poverty_white, digits = 4)
mean_poverty_nonwhite_str <- fmt(x = mean_poverty_nonwhite, digits = 4)
mean_poverty_gop_str <- fmt(x = mean_poverty_gop, digits = 4)
mean_poverty_nongop_str <- fmt(x = mean_poverty_nongop, digits = 4)

sd_poverty_white_str <- fmt(x = sd_poverty_white, digits = 4)
sd_poverty_nonwhite_str <- fmt(x = sd_poverty_nonwhite, digits = 4)
sd_poverty_gop_str <- fmt(x = sd_poverty_gop, digits = 4)
sd_poverty_nongop_str <- fmt(x = sd_poverty_nongop, digits = 4)

pval_poverty_white_str <- fmt(x = two_p_deandre_poverty_white, digits = 4)
pval_poverty_nonwhite_str <- fmt(x = two_p_deandre_poverty_nonwhite, digits = 4)
pval_poverty_gop_str <- fmt(x = two_p_deandre_poverty_gop, digits = 4)
pval_poverty_nongop_str <- fmt(x = two_p_deandre_poverty_nongop, digits = 4)

N_poverty_white_str <- format_int(x = N_poverty_white)
N_poverty_nonwhite_str <- format_int(x = N_poverty_nonwhite)
N_poverty_gop_str <- format_int(x = N_poverty_gop)
N_poverty_nongop_str <- format_int(x = N_poverty_nongop)

table_8 <- sprintf(fmt = "\\caption{Estimated effects of Black source on support for income-conscious policy by race and party of respondents}
\\centering 
\\begin{tabular}{@{\\extracolsep{5pt}}lcccc} 
\\\\[-1.8ex]\\hline 
\\hline \\\\[-1.8ex] 
 & \\multicolumn{4}{c}{\\textit{Dependent variable:}} \\\\ 
\\cline{2-5} 
\\\\[-1.8ex] & \\multicolumn{4}{c}{Income-conscious Policy Support}  \\\\ 
\\\\[-1.8ex] & White & non-White & GOP & non-GOP \\\\ 
\\hline \\\\[-1.8ex] 
Black Source Cue & $%s$ & $%s$ & $%s$ & $%s$ \\\\ 
White source cue (control) mean & $%s$ & $%s$ & $%s$ & $%s$ \\\\ 
White source cue (control) SD & $%s$ & $%s$ & $%s$ & $%s$ \\\\ 
RI $p$-value (two-sided) & $%s$ & $%s$ & $%s$ & $%s$ \\\\ 
Number of observations & $%s$ & $%s$ & $%s$ & $%s$ \\\\ 
\\hline 
\\hline 
\\textit{Note:} & \\multicolumn{4}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\\\ 
\\end{tabular}",
                   diff_poverty_white_str,
                   diff_poverty_nonwhite_str,
                   diff_poverty_gop_str,
                   diff_poverty_nongop_str,
                   mean_poverty_white_str,
                   mean_poverty_nonwhite_str,
                   mean_poverty_gop_str,
                   mean_poverty_nongop_str,
                   sd_poverty_white_str,
                   sd_poverty_nonwhite_str,
                   sd_poverty_gop_str,
                   sd_poverty_nongop_str,
                   pval_poverty_white_str,
                   pval_poverty_nonwhite_str,
                   pval_poverty_gop_str,
                   pval_poverty_nongop_str,
                   N_poverty_white_str,
                   N_poverty_nonwhite_str,
                   N_poverty_gop_str,
                   N_poverty_nongop_str)

writeLines(text = table_8, con = "code_and_output/tables/table_8.tex")

rm(list = setdiff(ls(), c("data", "fmt", "format_int", "n_sims")))
gc()